/**
 * Project Name:SinaSports
 * File Name:ProtocolTask.java
 * Package Name:cn.com.sina.sports.task
 * Date:2013-12-27下午4:45:58
 * Copyright (c) 2013, hushuan@staff.sina.com.cn All Rights Reserved.
 *
 */

package artid.com.arttogoa.task;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.params.HttpClientParams;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;

import artid.com.arttogoa.parser.BasicParser;
import artid.com.arttogoa.util.NetUtils;

/**
 * ClassName:ProtocolTask <br/>
 * Date: 2013-12-27 下午4:45:58 <br/>
 * 
 * @author hushuan
 * @version
 * @since JDK 1.6
 * @see
 */
public class ProtocolTask extends AsyncTask<BasicParser, BasicParser, Boolean> {
	OnProtocolTaskListener ml;
	private HttpClient mHttpClient;
	private HttpParams httpParams;
	// private HttpClient mHttpClient = HttpClientManager.getHttpClient();

	public ProtocolTask() {

	}

	public void setOnProtocolTaskListener(OnProtocolTaskListener l) {
		ml = l;
	}

	public HttpClient getHttpClient() {
		// 创建 HttpParams 以用来设置 HTTP 参数（这一部分不是必需的）
		httpParams = new BasicHttpParams();
		// 设置连接超时和 Socket 超时，以及 Socket 缓存大小
		HttpConnectionParams.setConnectionTimeout(httpParams, 20 * 1000);
		HttpConnectionParams.setSoTimeout(httpParams, 20 * 1000);
		HttpConnectionParams.setSocketBufferSize(httpParams, 8192);
		// 设置重定向，缺省为 true
		HttpClientParams.setRedirecting(httpParams, true);
		// 设置 user agent
		String userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2) Gecko/20100115 Firefox/3.6";
		HttpProtocolParams.setUserAgent(httpParams, userAgent);
		// 创建一个 HttpClient 实例
		// 注意 HttpClient httpClient = new HttpClient(); 是Commons HttpClient
		// 中的用法，在 Android 1.5 中我们需要使用 Apache 的缺省实现 DefaultHttpClient
		mHttpClient = new DefaultHttpClient(httpParams);
		return mHttpClient;
	}

	@Override
	protected Boolean doInBackground(BasicParser... params) {
		HttpResponse response;
		for (BasicParser parser : params) {
			if (null == parser) {
				continue;
			}
			String json = "";
			mHttpClient = getHttpClient();
			try {
				HttpUriRequest request = parser.getHttpUriRequest();
//				mHttpClient.getParams().setParameter(
//						CoreConnectionPNames.CONNECTION_TIMEOUT, 14000);
//				// 读取超时
//				mHttpClient.getParams().setParameter(
//						CoreConnectionPNames.SO_TIMEOUT, 10000);
				response = mHttpClient.execute(request);
//				Config.i("http status : " + response.getStatusLine());
				json = EntityUtils.toString(response.getEntity()); 
//				HttpEntity resEntity = response.getEntity();
//				InputStream content = resEntity.getContent();
//				InputStreamReader reader = new InputStreamReader(content,
//						"UTF-8");
//				char[] buff = new char[1024];
//				int length = 0;
//				while ((length = reader.read(buff)) != -1) {
//					json = json + new String(buff, 0, length);
//				}
				json = NetUtils.decodeUnicode(json);
				// json = HttpClientManager.getStringContent(response);
				
				System.out.println(json);
				parser.parse(json);
				// Config.i("httpResponse content : " + json);
			} catch (ClientProtocolException e) {
				parser.getHttpUriRequest().abort();
				// parser.parse(json);
			} catch (IOException e) {
				parser.getHttpUriRequest().abort();
				// parser.parse(json);
			}
			publishProgress(parser);
		}
		return true;
	}

	@Override
	protected void onProgressUpdate(BasicParser... values) {
		super.onProgressUpdate(values);
		if (null != ml) {
			ml.onPostExecute(values[0]);
		}
	}

}
